
// 清楚微信页面底部左右导航箭头
$(function(){
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
	    WeixinJSBridge.call('hideToolbar');
	    WeixinJSBridge.call('hideOptionMenu');
	});
})

//扩展方法
;(function(jQuery,window){
		var $ = jQuery;
		var win = window;
		$.resizeArr = [];
		$.swdata = {};
		$.swFn = {};
		$.extend({
			//向数组添加数据,且返回改数据
			pushFn:function(x){
				 $.resizeArr.push(x);
				 return x; 
			}
		});
		$.swFn = {
			  //使元素高度等于其宽度
			  fang:function(s,x,y){ 
				  	if($(s).length == 0){ return $();}
				  	if(arguments.length === 1){
				  		$(s).height( $(s).width())
				  	}else if(arguments.length === 3){
				  		$(s).height( parseInt($(s).width()*x/y) );
				  	}
				  	return $(s);				  		  	
			  },
			  //若子元素的高度小于
			  posit:function(s){
			  	  if($(s).length == 0){ return false;}
			      var dom1 = $(s); 
			      var dom2 = $(s).parent();   
			      if( dom1.height() < dom2.height() ){
			         dom1.css({ marginTop:parseInt((dom2.height()- dom1.height())/3)})
			      }else{
			         dom1.css({ marginTop:0})
			      }
			      return $(s);
			  },		   
		    // 图片居中显示                                  
		    imgCenter:function(s){ 
		    	  if($(s).length == 0){ return false;}
				    function go(t,w,h){                                     
				        var width = t.parentNode.offsetWidth;
				        var height = t.parentNode.offsetHeight;
				        var b1 = width/height;
				        var b2 = w/h;    
				        if(b1 <= b2){
				           t.style.height = height + "px";
				           t.style.width = (height/h)*w + "px";
				           t.style.marginLeft = -((height/h)*w-width)/2 + "px";
				           t.style.marginTop = 0;
				        }else{
				           t.style.width = width + "px";
				           t.style.height = (width/w)*h + "px";
				           t.style.marginTop = -((width/w)*h-height)/2 + "px";
				           t.style.marginLeft = 0;
				        }   
				    }
				    var imgs = [];                                               
				    $(s).each(function(k,v){
				        clearInterval(v.timer);
				        v.timer = setInterval(function(){             
				            if(v.offsetHeight > 0){
				                v.cenObj = {
				                  width:v.width,
				                  height:v.height
				                };
				                go(v,v.width,v.height);
				                imgs.push(v); 
				                clearInterval(v.timer);
				            }
				        },30)
				    })                                                                  
				    return $(s);
				},
				//获取添加图片的 URL
				getImgurl:function(dom){
						var fileName = dom.value;   
						if (!fileName.match(/.jpg|.gif|.png|.bmp/i)) {   
						    alert('您上传的图片格式不正确，请重新选择！');   
						    return false;   
						}   
						var file = dom.files[0];
						if (!!file) {   
						    var myURL = window.URL || window.webkitURL;
						    var url = myURL.createObjectURL(file);
						    return url;
						} 
				}



		}
})(jQuery,window)



/*
初始化执行且随屏幕变化,实例如下:
<div class="zhengfang">
   <img src="" dpoint="center">
</div>
1.只设置div的宽度,使其的高度动态等于其宽度.  添加类 class="zhengfang"  返回该div jQuery对象
2.使img不变形刚好撑满父元素,且中心点居中.    添加属性 dpoint="center"  返回该img jQuery对象
3.将执行函数放入数组,给window添加监听事件,数组中函数
*/
$(function(){
    $.pushFn(
        function(){
        	  $.swFn.imgCenter($.swFn.fang(".zhengfang").find("img[dpoint=center]"));            
        }
    )();

		//下拉框点击
		$(".p-head-botm ul").find("a").click(function () {    
        var dom = $(".selectList");                                                      
				if(dom.css("display")=="none"){
		         if( $(this).attr("dc") ){
					        if( $(this).attr("dc")==="jieshao"){          
					            dom.find(".jieshao").show();
					            dom.find(".tuwen").hide();

					        }else if( $(this).attr("dc")==="tuwen" ){
					            dom.find(".jieshao").hide();
					            dom.find(".tuwen").show();
					        }	 
						      dom.height(2000);
			            $(".page-wrap").css("overflow-y","hidden");
			            dom.fadeIn();     	
		         }	

				}else{
		        if( $(this).attr("dc")==="jieshao"){          
		            dom.find(".jieshao").fadeIn();
		            dom.find(".tuwen").fadeOut();

		        }else if( $(this).attr("dc")==="tuwen" ){
		            dom.find(".jieshao").fadeOut();
		            dom.find(".tuwen").fadeIn();
		        }else{
		            dom.fadeOut(function(){ dom.height(0) });
		            $(".page-wrap").css("overflow-y","auto");		        	
		        }
				}
		})
		$(".selectList").click(function(){ 
		    var $this = $(this);
		    $this.fadeOut(function(){ $this.height(0) });
		    $(".page-wrap").css("overflow-y","auto");
		})


		//登录框
		$(document).on("click",".login",function(){
			$.modal({
				  title: "用户登录",
				  text:'\
					<form id="login" action="">\
						<label class="shuru">\
							<span>账号：</span>\
							<input type="text" name="user" placeholder="输入用户名">\
						</label>	\
						<label class="shuru">\
							<span>密码：</span> \
							<input type="password" name="pass" placeholder="输入密码">\
						</label>	\
					    <label class="radio act">\
							<input type="radio" name="role" value="1" checked="checked">\
							<i class="weui-icon-success"></i>\
							教师\
						</label>\
						<label class="radio">\
							<input type="radio" name="role" value="2">\
							<i class="weui-icon-circle"></i>\
							家长\
						</label>\
						<label class="radio">\
							<input type="radio" name="role" value="3">\
							<i class="weui-icon-circle"></i>\
							学生\
						</label>\
					</form>',
				  buttons: [
				    { text: "取消", className: "default", onClick: function(){ console.log(3)} },
				    { text: "提交", onClick: function(){ 
				    	console.log( $("#login").serialize())
				    	// $.ajax({

				    	// })
				    }}
				 ]
			});
		});

		$(document).on("click","#login .radio",function(){
			$(this).parent().find(".radio").removeClass("act")
			$(this).parent().find("i").removeClass("weui-icon-success").addClass("weui-icon-circle");
		    $(this).addClass("act");
			$(this).find("i")[0].className = "weui-icon-success";
		});

})
window.onresize = function(){
		$.each($.resizeArr,function(k,v){
			  v();
		})
};

//点击事件删除父元素, 只需给改点击元素添加类 class="close" 
$(document).on("click",".close",function(){                   
		$(this).parents(".close-re").remove();
})


